package com.hua.yuan.ui;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hua.yuan.common.BaseActivity;
import com.hua.yuan.R;
import com.hua.yuan.provider.user.IUserProvider;
import com.hua.yuan.provider.user.User;
import com.hua.yuan.userlogin.UserLoginModule;
import com.hua.yuan.userlogin.business.callback.IUserLoginCallback;
import com.hua.yuan.userlogin.business.result.UserSigBean;
import com.hua.yuan.util.SystemUtils;
import com.hua.yuan.util.ToastUtil;

import retrofit2.http.PATCH;

/**
 * Created by huangyang on 2018/5/9.
 * 登录界面
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener, IUserLoginCallback {

    private ImageView backImage;
    private TextView titleView;

    private EditText loginInputPhone;
    private EditText loginInputPwd;
    private TextView loginButton;
    private TextView loginForgetPwdButton;

    private RelativeLayout loginLoadView;

    private final String TAG = this.getClass().getName();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
        initData();

    }


    private void initView() {

        backImage = findViewById(R.id.backImage);
        titleView = findViewById(R.id.titleView);

        loginInputPhone = findViewById(R.id.loginInputPhone);
        loginInputPwd = findViewById(R.id.loginInputPwd);
        loginButton = findViewById(R.id.loginButton);
        loginForgetPwdButton = findViewById(R.id.loginForgetPwdButton);

        loginLoadView = findViewById(R.id.loginLoadView);


    }

    private void initData() {

        backImage.setOnClickListener(this);
        loginButton.setOnClickListener(this);
        loginForgetPwdButton.setOnClickListener(this);

        titleView.setText("登录");

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.backImage:
                //返回
                finish();
                break;
            case R.id.loginButton:
                //登录
                loginFun();
                break;
            case R.id.loginForgetPwdButton:
                //忘记密码
                Intent intent = new Intent(this, ForgetPwdActivity.class);
                jumpActivity(intent);
                break;
        }

    }

    private void jumpActivity(Intent pIntent) {
        startActivity(pIntent);
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        IUserProvider userProvider=UserLoginModule.getInstance().getProvider();
        //注册完以后就有值
        boolean isLogin=userProvider.getIsLogin();
        if(!isLogin){
            return;
        }
        //如果未注册 就登陆
        UserLoginModule.getInstance().getUserLogin().userLogin(userProvider.getMobilePhone(),userProvider.getPassword(), this);
    }

    private void loginFun() {

        final String phone = loginInputPhone.getText().toString().trim();
        String pwd = loginInputPwd.getText().toString().trim();

        if (phone.length() == 0) {
            ToastUtil.showShortToast("请输入手机号");
            return;
        }

        if (phone.length() != 11) {
            ToastUtil.showShortToast("请输入正确的手机号");
            return;
        }

        if (pwd.length() == 0) {
            ToastUtil.showShortToast("请输入密码");
            return;
        }

        if (phone.length() < 6) {
            ToastUtil.showShortToast("密码位数不能少于6位");
            return;
        }
        SystemUtils.hideInputmethod(loginInputPwd);
        loginLoadView.setVisibility(View.VISIBLE);
        UserLoginModule.getInstance().getUserLogin().userLogin(phone, pwd, this);
    }

    @Override
    public void onResult(@NonNull final UserSigBean pUserSig) {

        SystemUtils.isMainThread(TAG);
        //防止activity 泄露后产生UI更新崩溃
        if (isFinishing()) {
            return;
        }

        if (pUserSig == null) {
            return;
        }
        //执行模块登录
        if (loginLoadView != null) {
            loginLoadView.setVisibility(View.GONE);
        }
        //登录成功，关闭页面
        finish();
    }

    @Override
    public void onError(final Throwable e) {
        SystemUtils.isMainThread(TAG);
        ToastUtil.showShortToast("登录失败");
        //防止activity 泄露后产生UI更新崩溃
        if (isFinishing()) {
            return;
        }
        if (loginLoadView != null) {
            loginLoadView.setVisibility(View.GONE);
        }
    }
}
